<?php
class User_model extends DS_Model
{
    private $model;

    function __construct()
    {
        parent::__construct();
        $this->load->helpers('string_helper');
        $this->model = 'user';
    }

    //根据条件查询一条信息
    public function onceInfo($where, $select)
    {
        $res = $this->db->where($where)->select($select)->order_by('id desc')->limit(1)->get($this->model)->row_array();
        return $res;
    }

    //查询总的条数
    public function countLog($where)
    {
        $count = $this->db->where($where)->from($this->model)->count_all_results();
        return $count;
    }

    //添加
    public function insertInfo($data)
    {
        $res = $this->db->insert($this->model, $data);
        return $res;
    }

    //修改
    public function updateInfo($data, $where)
    {
        $res = $this->db->update($this->model, $data, $where);
        return $res;
    }

    public function getInfo($where)
    {
        $res = $this->db->where($where)->select("*")->order_by('id desc')->limit(1)->get($this->model)->row_array();
        return $res;
    }

    //根据条件查询所有信息
    public function allInfoorder($where, $select, $order_by)
    {
        $res = $this->db->where($where)->select($select)->order_by($order_by)->get($this->model)->result_array();
        return $res;
    }

    /*
     *发送短信
     * phone手机号
     * type请求类型 1注册，2忘记密码，3旧手机号，4新手机号
     */
    public function setSendSms($phone,$type,$user_id){
        if(!in_array($type,array(1,2,3,4))){
            $info=array('code'=>'400','msg'=>'请求类型不正确');
            return $info;
        }
        if($type==1){//注册
            $count=$this->countLog(array('phone'=>$phone,'phone_valid'=>1));
            if($count>0){
                $info=array('code'=>'400','msg'=>'该手机号已被注册');
                return $info;
            }
            $count_exist=$this->countLog(array('phone'=>$phone));
            if($count_exist==0){//没有发送过验证码
                $verify=123456;
                $data=array(
                    'phone'=>$phone,
                    'phone_code'=>$verify,
                    'phone_code_time'=>now()
                );
                $v=$this->insertInfo($data);
                if($v){
                    //添加发送短信日志
//                    $insert=array(
//                        'type'=>1,
//                        'code'=>$verify,
//                        'content'=>$text,
//                        'phone'=>$phone,
//                        'send_status'=>1,
//                        'timeline'=>now()
//                    );
//                    $this->db->insert('note',$insert);
                    $info=array('code'=>'200','msg'=>'短信验证码发送成功');
                    return $info;
                }else{
                    //添加发送短信日志
//                    $insert=array(
//                        'type'=>1,
//                        'code'=>$verify,
//                        'content'=>$text,
//                        'phone'=>$phone,
//                        'send_status'=>2,
//                        'timeline'=>now()
//                    );
//                    $this->db->insert('note',$insert);
                    $info=array('code'=>'400','msg'=>'短信验证码发送失败');
                    return $info;
                }
            }else{//发送过验证码
                $res=$this->onceInfo(array('phone'=>$phone,'phone_valid'=>0),'phone_code_time');
                if(now()-$res['phone_code_time']<60){
                    $info=array('code'=>'400','msg'=>'提交过于频繁,'.(60-(now()-$res['phone_code_time'])).'s之后在操作');
                    return $info;
                }
                $verify=123456;
                $data=array(
                    'phone_code'=>$verify,
                    'phone_code_time'=>now()
                );
                $v=$this->updateInfo($data,array('phone'=>$phone,'phone_valid'=>0));
                if($v){
                    //添加发送短信日志
//                    $insert=array(
//                        'type'=>1,
//                        'code'=>$verify,
//                        'content'=>$text,
//                        'phone'=>$phone,
//                        'send_status'=>1,
//                        'timeline'=>now()
//                    );
//                    $this->db->insert('note',$insert);
                    $info=array('code'=>'200','msg'=>'短信验证码发送成功');
                    return $info;
                }else{
                    //添加发送短信日志
//                    $insert=array(
//                        'type'=>1,
//                        'code'=>$verify,
//                        'content'=>$text,
//                        'phone'=>$phone,
//                        'send_status'=>2,
//                        'timeline'=>now()
//                    );
//                    $this->db->insert('note',$insert);
                    $info=array('code'=>'400','msg'=>'短信验证码发送失败');
                    return $info;
                }
            }
        }elseif($type==2){
            $count=$this->countLog(array('phone'=>$phone,'phone_valid'=>1));
            if($count==0){
                $info=array('code'=>'400','msg'=>'该手机号未注册');
                return $info;
            }
            $res=$this->onceInfo(array('phone'=>$phone,'phone_valid'=>1),'id,find_pwd_code_time');
            if(!empty($res['find_pwd_code_time']) && (now()-$res['find_pwd_code_time']<60)){
                $info=array('code'=>'400','msg'=>'提交过于频繁,'.(60-(now()-$res['find_pwd_code_time'])).'s之后在操作');
                return $info;
            }
            $verify=123456;
            $data = array(
                'find_pwd_code_time' => now(),
                'find_pwd_code' => $verify,
            );
            $v=$this->updateInfo($data,array('phone'=>$phone,'phone_valid'=>1,'id'=>$res['id']));
            if($v){
                //添加发送短信日志
//                    $insert=array(
//                        'type'=>2,
//                        'code'=>$verify,
//                        'content'=>$text,
//                        'phone'=>$phone,
//                        'send_status'=>1,
//                        'timeline'=>now()
//                    );
//                    $this->db->insert('note',$insert);
                $info=array('code'=>'200','msg'=>'短信验证码发送成功');
                return $info;
            }else{
                //添加发送短信日志
//                    $insert=array(
//                        'type'=>2,
//                        'code'=>$verify,
//                        'content'=>$text,
//                        'phone'=>$phone,
//                        'send_status'=>2,
//                        'timeline'=>now()
//                    );
//                    $this->db->insert('note',$insert);
                $info=array('code'=>'400','msg'=>'短信验证码发送失败');
                return $info;
            }
        }elseif($type==3){
            if(!$user_id){
                $info=array('code'=>'400','msg'=>'您的登录已经失效，请重新登录');
                return $info;
            }
            $count=$this->countLog(array('id'=>$user_id,'phone_valid'=>1,'is_delect'=>0));
            if($count==0){
                $info=array('code'=>'400','msg'=>'该手机号未注册');
                return $info;
            }
            $res=$this->onceInfo(array('id'=>$user_id,'phone_valid'=>1),'id,phone_code_time');
            if(!empty($res['phone_code_time']) && (now()-$res['phone_code_time']<60)){
                $info=array('code'=>'400','msg'=>'提交过于频繁,'.(60-(now()-$res['phone_code_time'])).'s之后在操作');
                return $info;
            }
            $verify=123456;
            $data=array(
                'phone_code'=>$verify,
                'phone_code_time'=>now()
            );
            $v=$this->updateInfo($data,array('phone'=>$phone,'phone_valid'=>1,'id'=>$res['id']));
            if($v){
                //添加发送短信日志
//                    $insert=array(
//                        'type'=>3,
//                        'code'=>$verify,
//                        'content'=>$text,
//                        'phone'=>$phone,
//                        'send_status'=>1,
//                        'timeline'=>now()
//                    );
//                    $this->db->insert('note',$insert);
                $info=array('code'=>'200','msg'=>'短信验证码发送成功');
                return $info;
            }else{
                //添加发送短信日志
//                    $insert=array(
//                        'type'=>3,
//                        'code'=>$verify,
//                        'content'=>$text,
//                        'phone'=>$phone,
//                        'send_status'=>2,
//                        'timeline'=>now()
//                    );
//                    $this->db->insert('note',$insert);
                $info=array('code'=>'400','msg'=>'短信验证码发送失败');
                return $info;
            }
        }elseif($type==4){
            if(!$user_id){
                $info=array('code'=>'400','msg'=>'您的登录已经失效，请重新登录');
                return $info;
            }
            $count=$this->countLog(array('phone'=>$phone,'phone_valid'=>1,'is_delect'=>0));
            if($count>0){
                $info=array('code'=>'400','msg'=>'该手机号已注册');
                return $info;
            }
            $res=$this->onceInfo(array('id'=>$user_id),'id,change_phone_code_time');
            if(empty($res)){
                $info=array('code'=>'400','msg'=>'登录已失效');
                return $info;
            }
            if(!empty($res['change_phone_code_time']) && (now()-$res['change_phone_code_time']<60)){
                $info=array('code'=>'404','msg'=>'提交过于频繁,'.(60-now()-$res['change_phone_code_time']).'s之后在操作');
                return $info;
            }
            $verify=123456;
            $data = array(
                'change_phone'=>$phone,
                'change_phone_code_time' => now(),
                'change_phone_code' => $verify,
            );
            $v=$this->updateInfo($data,array('id'=>$user_id));
            if($v){
                //添加发送短信日志
//                    $insert=array(
//                        'type'=>4,
//                        'code'=>$verify,
//                        'content'=>$text,
//                        'phone'=>$phone,
//                        'send_status'=>1,
//                        'timeline'=>now()
//                    );
//                    $this->db->insert('note',$insert);
                $info=array('code'=>'200','msg'=>'短信验证码发送成功');
                return $info;
            }else{
                //添加发送短信日志
//                    $insert=array(
//                        'type'=>4,
//                        'code'=>$verify,
//                        'content'=>$text,
//                        'phone'=>$phone,
//                        'send_status'=>2,
//                        'timeline'=>now()
//                    );
//                    $this->db->insert('note',$insert);
                $info=array('code'=>'400','msg'=>'短信验证码发送失败');
                return $info;
            }
        }

    }

    /*
     * 检测验证码
     */

    public function check_sms($phone,$type,$user_id,$sms){
        if(!in_array($type,array(1,2,3,4))){
            $info=array('code'=>'400','msg'=>'请求类型不正确');
            return $info;
        }
        if($type==1){
            $count=$this->countLog(array('phone'=>$phone));
            if($count==0){
                $info=array('code'=>'400','msg'=>'请获取验证码');
                return $info;
            }
            $count=$this->countLog(array('phone'=>$phone,'phone_valid'=>1));
            if($count>0){
                $info=array('code'=>'400','msg'=>'该手机号码已注册');
                return $info;
            }
            $res=$this->onceInfo(array('phone'=>$phone),'phone_code,phone_code_time');
            if(empty($res['phone_code'])){
                $info=array('code'=>'400','msg'=>'请获取验证码');
                return $info;
            }
            if(now()-$res['phone_code_time']>1800){
                $info=array('code'=>'400','msg'=>'未发送验证码或者验证码已过期，请重新发送');
                return $info;
            }
            if($sms!=$res['phone_code']){
                $info=array('code'=>'400','msg'=>'手机验证码错误');
                return $info;
            }
            //$this->db->update('note',array('use'=>1),array('code'=>$sms,'phone'=>$phone,'type'=>$type));
            $info=array('code'=>'200','msg'=>'校验成功');
            return $info;
        }elseif($type==2){
            $count=$this->countLog(array('phone'=>$phone,'phone_valid'=>1));
            if($count==0){
                $info=array('code'=>'400','msg'=>'该手机号码注册');
                return $info;
            }
            $res=$this->onceInfo(array('phone'=>$phone,'phone_valid'=>1),'find_pwd_code,find_pwd_code_time');
            if(empty($res['find_pwd_code'])){
                $info=array('code'=>'400','msg'=>'请获取验证码');
                return $info;
            }
            if(now()-$res['find_pwd_code_time']>1800){
                $info=array('code'=>'400','msg'=>'未发送验证码或者验证码已过期，请重新发送');
                return $info;
            }
            if($sms!=$res['find_pwd_code']){
                $info=array('code'=>'400','msg'=>'手机验证码错误');
                return $info;
            }
            //$this->db->update('note',array('use'=>1),array('code'=>$sms,'phone'=>$phone,'type'=>$type));
            $info=array('code'=>'200','msg'=>'校验成功');
            return $info;
        }elseif($type==3){
            if(!$user_id){
                $info=array('code'=>'400','msg'=>'您的登录已经失效，请重新登录');
                return $info;
            }
            $count=$this->countLog(array('phone'=>$phone,'phone_valid'=>1,'is_delect'=>0));
            if($count==0){
                $info=array('code'=>'400','msg'=>'该手机号未注册');
                return $info;
            }
            $res=$this->onceInfo(array('phone'=>$phone),'phone_code,phone_code_time');
            if(empty($res['phone_code'])){
                $info=array('code'=>'400','msg'=>'请获取验证码');
                return $info;
            }
            if(now()-$res['phone_code_time']>1800){
                $info=array('code'=>'400','msg'=>'未发送验证码或者验证码已过期，请重新发送');
                return $info;
            }
            if($sms!=$res['phone_code']){
                $info=array('code'=>'400','msg'=>'手机验证码错误');
                return $info;
            }
           // $this->db->update('note',array('use'=>1),array('code'=>$sms,'phone'=>$phone,'type'=>$type));
            $info=array('code'=>'200','msg'=>'校验成功');
            return $info;
        }elseif($type==4){
            if(!$user_id){
                $info=array('code'=>'400','msg'=>'您的登录已经失效，请重新登录');
                return $info;
            }
            $count=$this->countLog(array('phone'=>$phone,'phone_valid'=>1,'is_delect'=>0));
            if($count>0){
                $info=array('code'=>'400','msg'=>'该手机号已注册');
                return $info;
            }
            $res=$this->onceInfo(array('id'=>$user_id),'change_phone_code_time,change_phone_code,phone');
            if(empty($res)){
                $info=array('code'=>'400','msg'=>'登录已失效');
                return $info;
            }
            if(empty($res['change_phone_code'])){
                $info=array('code'=>'400','msg'=>'请获取验证码');
                return $info;
            }
            if(now()-$res['change_phone_code_time']>1800){
                $info=array('code'=>'400','msg'=>'未发送验证码或者验证码已过期，请重新发送');
                return $info;
            }
            if($sms!=$res['change_phone_code']){
                $info=array('code'=>'400','msg'=>'手机验证码错误');
                return $info;
            }
            //$this->db->update('note',array('use'=>1),array('code'=>$sms,'phone'=>$phone,'type'=>$type));
            $data=array(
                'phone'=>$phone,
                'change_phone'=>$phone,
                'old_phone'=>$res['phone']
            );
            $res=$this->updateInfo($data,array('id'=>$user_id));
            if($res){
                $info=array('code'=>'200','msg'=>'校验成功');
                return $info;
            }else{
                $info=array('code'=>'200','msg'=>'校验失败');
                return $info;
            }
        }
    }

}
?>